//获取工具类的应用实例  
const util = require('../../../utils/util.js');
const App = getApp();



Page({
  data: {
    imgwidth: 0,
    imgheight: 0,
    windowWidth: wx.getSystemInfoSync().screenWidth,
    windowHeight: wx.getSystemInfoSync().windowHeight,
    goods: { item: ['a', 'b', 'c', 'd', 'e'] },
    params: {
      page: 1,
      limit: 8
    }

  },

  //初始化数据加载
  onLoad() {
    this.setData({
      goods: { item: ['到手价', 'yellow', 'blue', 'green', 'red', '淘宝', '精选'] },
    })
  },

  //初始化数据
  initData() {
    const type = this.data.type
    const keyword = this.data.keyword
    this.setData({
      goods: {
        items: [],
        params: {
          page: 1,
          limit: 10,
          type: type,
          keyword: keyword,
        },
        paginate: {}
      }
    })
  },

  //点击跳转到商品详情页面
  getList() {
    const goods = this.data.goods
    const params = goods.params

    //调用接口数据并格式处理
    this.goods.queryAsync(params)
      .then(res => {
        const data = res.data
        console.log(data)
        if (data.meta.code == 0) {
          data.data.items.forEach(n => n.thumb_url = App.renderImage(n.images[0] && n.images[0].path))
          goods.items = [...goods.items, ...data.data.items]
          goods.paginate = data.data.paginate
          goods.params.page = data.data.paginate.next
          goods.params.limit = data.data.paginate.perPage
          this.setData({
            goods: goods,
            'prompt.hidden': goods.items.length,
          })
        }
      })
  },


  //拉到底部刷新
  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    this.initData()
    this.getList()
  },

  //触底刷新
  onReachBottom() {
    console.info('onReachBottom')
    //if (!this.data.goods.paginate.hasNext) return
    this.getList()
  },


  //商品图片适配
  imageLoad: function (e) {
    var windowWidth = this.data.windowWidth;
    var $width = e.detail.width;    //获取图片真实宽度  
    var $height = e.detail.height;
    var ratio = $width / $height;   //图片的真实宽高比例  
    //设置图片显示宽度,适配0.92的比例，
    var viewWidth = windowWidth*0.92;             
    var viewHeight = viewWidth / ratio;    //计算的高度值     
    this.setData({
      imgwidth: viewWidth,
      imgheight: viewHeight
    })
  },

})